home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Development Kits / HyperCard Related / APDA HyperCard Toolkits / CD Audio Toolkit 1.0 / Source / CDPlay1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-07  |  5.3 KB  |  210 lines  |  [TEXT/MPS ]

  1. /*
  2.     CDPlay1 - An XCMD to play an audio track and stop.
  3.     ©Apple Computer, Inc. 1988
  4.     All Rights Reserved.
  5.     
  6.     88/11/15    BL°B    First Version
  7.  
  8.     To compile and link this file using Macintosh Programmer's Workshop,
  9.  
  10.     C -q2 CDPlay1.c
  11.     link -sn Main=CDPlay1 -sn STDIO=CDPlay1 ∂
  12.          -sn INTENV=CDPlay1 -rt XFCN=42 ∂
  13.          -m CDPlay1 CDPlay1.c.o "{CLibraries}"CRuntime.o ∂
  14.          "{CLibraries}"StdCLib.o ∂
  15.          -o HyperCommands
  16.          
  17.     This link directive puts the XCMD in the file "HyperCommands".
  18.     Substitute the name of the stack you want it in.  To move XCMDs
  19.     between stacks, use ResEdit.  They can be in an individual stack,
  20.     the Home stack, the HyperCard application, or the System File.
  21.     
  22. */
  23.  
  24. #include <cd.h>
  25.  
  26. /* prototype definitions for functions */
  27. void    ExtractTrackNo(char *, short *);
  28. OSErr    APlay(XCmdBlockPtr, short, short);
  29. OSErr    AStop(short, short);
  30.  
  31. /* **** WARNING:  DO NOT USE GLOBAL VARIABLES! **** */
  32.  
  33.  
  34. /************************************************************************
  35.  *
  36.  *  Function:        CDPlay1
  37.  *
  38.  *  Purpose:        play an audio track
  39.  *
  40.  *  Returns:        result of driver call to play track
  41.  *                    normally 0, but could have parameter error or
  42.  *                    other error if non-existent track is specified
  43.  *
  44.  *  Side Effects:
  45.  *
  46.  *  Description:    We need one parameter, the track number.  Get
  47.  *                    the global ioRefNum that we set by
  48.  *                    previously calling CDOpen().  Extract the 
  49.  *                    track number and play it.
  50.  *
  51.  ************************************************************************/
  52. pascal void
  53. CDPlay1(paramPtr)
  54. XCmdBlockPtr    paramPtr;
  55. {
  56.     Str31    returnString;
  57.     OSErr    result;
  58.     short    trackNumber;
  59.     short    ioRefNum;
  60.     Handle    refHandle;
  61.     
  62.     /* Must be one parameter */
  63.     if ((paramPtr->paramCount) != 1)
  64.     {
  65.         /* Report error in parameters by returning -1 */
  66.         NumToStr(paramPtr, (long) -1, &returnString);
  67.         paramPtr->returnValue = PasToZero(paramPtr, (StringPtr) &returnString);
  68.         return;
  69.     }
  70.     
  71.     /* Get the global ioRefNum and convert it. */
  72.     refHandle = GetGlobal(paramPtr, GLOBALNAME);
  73.     ioRefNum = atoi(*(refHandle));
  74.     DisposHandle(refHandle);
  75.     ioRefNum &= 0xFFFF;            /* remove vRefNum; not needed. */
  76.     
  77.     /* First param is track number.  Convert it to a pascal string */
  78.     ExtractTrackNo((char *)*(paramPtr->params[0]), &trackNumber);
  79.     
  80.     result = AStop(trackNumber, ioRefNum);
  81.     
  82.     if (result == noErr)
  83.         result = APlay(paramPtr, trackNumber, ioRefNum);
  84.     
  85.     /* Convert result to string & return it */
  86.     NumToStr(paramPtr, (long) result, &returnString);
  87.     paramPtr->returnValue = PasToZero(paramPtr, (StringPtr) &returnString);
  88. }
  89.  
  90. /************************************************************************
  91.  *
  92.  *  Function:        ExtractTrackNo
  93.  *
  94.  *  Purpose:        Extract track number in BCD from PString
  95.  *
  96.  *  Returns:        nothing
  97.  *
  98.  *  Side Effects:    *track gets a new value
  99.  *
  100.  *  Description:    Extract track number in BCD from Cstring "name".
  101.  *                    "name" is always of the form "XX", where XX
  102.  *                    ranges from "1"  to "99"
  103.  *                    (You can't have more than 99 tracks on a compact disc.
  104.  *                    Bet you didn't know that, did you?  Right in the "Yellow
  105.  *                    Book" specification of compact disc encoding.)
  106.  *
  107.  ************************************************************************/
  108. void
  109. ExtractTrackNo(name, track)
  110. char        *name;
  111. short        *track;
  112. {
  113.     short    t;
  114.         
  115.     t = 0;
  116.     while (*name != 0)
  117.     {
  118.         t *= 16;
  119.         t += *name - '0';
  120.         name++;
  121.     }
  122.     
  123.     *track = t;    
  124. }
  125.  
  126. /************************************************************************
  127.  *
  128.  *  Function:        APlay
  129.  *
  130.  *  Purpose:        start playing an audio track
  131.  *
  132.  *  Returns:        OSErr.  Probably either
  133.  *                        noErr        everything's hunky-dory!
  134.  *                        paramErr    you messed up the call somehow.
  135.  *
  136.  *  Side Effects:    starts play.  By default, this will play until the
  137.  *                    end of the disc.
  138.  *
  139.  *  Description:    The track that you pass in is the first track that you
  140.  *                    want to have play.
  141.  *
  142.  ************************************************************************/
  143. OSErr
  144. APlay(paramPtr, track, refNum)
  145. XCmdBlockPtr    paramPtr;
  146. short    track;
  147. short    refNum;
  148. {
  149.     struct {
  150.         short    addrFormat;
  151.         long    address;
  152.         short    stopAddress;
  153.         short    playMode;
  154.     }    csParam;
  155.     short        playMode;
  156.     Handle        refHandle;
  157.     
  158.     /* Get the global ioRefNum and convert it. */
  159.     refHandle = GetGlobal(paramPtr, PLAYMODE);
  160.     playMode = atoi(*(refHandle));
  161.     DisposHandle(refHandle);
  162.     
  163.     csParam.addrFormat = TRACKADDR;
  164.     csParam.address = track;    /* must be in BCD */
  165.     csParam.stopAddress = 0;
  166.     csParam.playMode = playMode;
  167.     return (Control(refNum, APLAY, &csParam));
  168. }
  169.  
  170.  
  171. /************************************************************************
  172.  *
  173.  *  Function:        AStop
  174.  *
  175.  *  Purpose:        stop playing an audio track
  176.  *
  177.  *  Returns:        OSErr.  Probably either
  178.  *                        noErr        everything's hunky-dory!
  179.  *                        paramErr    you messed up the call somehow.
  180.  *
  181.  *  Side Effects:    stops play.
  182.  *
  183.  *  Description:    The track that you pass in is the LAST track that you
  184.  *                    want to have play.  This means that if you wanted to
  185.  *                    play only one track, you'd pass the same value to
  186.  *                    this routine and APlay() [q.v.]
  187.  *
  188.  *                    Note that this routine isn't called in this sample,
  189.  *                    but it's included for your enjoyment.
  190.  *
  191.  ************************************************************************/
  192. OSErr
  193. AStop(track, refNum)
  194. short    track;
  195. short    refNum;
  196. {
  197.     struct {
  198.         short    addrFormat;
  199.         long    address;
  200.     }    csParam;
  201.     
  202.     csParam.addrFormat = TRACKADDR;
  203.     csParam.address = track;
  204.     return (Control(refNum, ASTOP, &csParam));
  205. }
  206.  
  207.  
  208. /* C routines for HyperCard callbacks */
  209. #include <XCmdGlue.inc.c>
  210.